<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;
use fast\Rsa;

/**
 * 示例接口
 */
class Gbook extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [ '*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        //设置跨域问题
        header("Access-Control-Allow-Origin:*");
       $this->key_pub = file_get_contents('pub.ini');
       $this->key_pri = file_get_contents('pri.ini');
       $this->rsa = new Rsa();//加密类
    }

    public function getd()
    {   
        // echo $this->key_pub;
        // echo( json_encode(['time'=>time(),'pub'=>$this->key_pub]));
        $this->success('ok', base64_encode(json_encode(['time'=>time(),'pub'=>$this->key_pub])));
        // $this->success('ok', ['time'=>time(),'pub'=>$this->key_pub]);
    }

    public function gett()
    {  
        echo time();//获取时间
        // $this->success('ok', base64_encode(json_encode(['time'=>time(),'pub'=>$this->key_pub])));
    }

    public function post1()
    {   
        $data['ip'] = $this->request->ip();//ip
        $date = date('Y-m-d');
        $date1 = strtotime($date);
        $ipnum = Db::name('ysjyy')->where('ip',$data['ip'])->where('createtime','>',$date1)->count();
        if($ipnum>=3){
            return "date";
        }

    // dump($this->request->param());
        // dump($_POST['htel']);
        // $data['name'] =$this->request->post('hname');
        // $data['tel'] =$this->request->post('htel');
        // $data['houses'] =$this->request->post('houses');
        // $data['area'] =$this->request->post('area');


        foreach ($this->request->param() as $key => $value) {
             // echo $key;
             $data1 = json_decode($key,true);
             // dump($data1);
            // dump($value);
        }
        // dump($data);
        $data['name'] =$data1['hname'];
        $data['tel'] =$data1['htel'];
        $data['houses'] =$data1['houses'];

        $data['area'] = $data1['area'];
        if(strpos($data1['area'],'_')){
            // dump();
            $data['area'] = str_replace('_','.',$data1['area']);
        }


        foreach ($data as $key => $value) {
            if(empty($value)){
                // dump($data);
                return 'no';
            }
        }


        $this->charu_data($data);

        // $this->request->param();
        
        // $this->success('返回成功', 'ok');
    }

    /**
     * 需要登录的接口
     *
     */
    public function post2()
    {   
        // dump($this->request->param());
        $param = $this->request->param();
        // dump($param['date']);
        $dedata = $this->rsa->privDecrypt($param['data']);
        if(!empty($dedata)){
            if(json_decode($dedata,true)){
                $data1 = json_decode($dedata,true);

                //解析后的数据



                //  if($data1['xiao'] == 'ot'){
                //     $data['tel'] =$data1['htel'];
                //     $data['area'] = $data1['area'];
                // }else if($data1['xiao'] == 'pc'){
                //     $data['name'] =$data1['hname'];
                //     $data['tel'] =$data1['htel'];
                //     $data['houses'] =$data1['houses'];
                //     $data['area'] = $data1['area'];
                //     $data['type'] = $data1['xiao'];
                // }else if($data1['xiao'] == 'mb'){
                //     $data['name'] =$data1['hname'];
                //     $data['tel'] =$data1['htel'];
                //     $data['houses'] =$data1['houses'];
                //     $data['area'] = $data1['area'];
                //     $data['type'] = $data1['xiao'];
                //     $data['style'] = $data1['style'];
                //     $data['ps'] = $data1['ps'];
                // }
                $name = ['name'=>'hname','tel'=>'htel','houses'=>'houses','area'=>'area','style'=>'style','type'=>'xiao','ps'=>'ps'];
                foreach($name as $key=>$val){
                        if(!empty($data1[$val])){
                            $data[$key] =$data1[$val];
                        }
                }

                // $data['name'] =$data1['hname'];
                // $data['tel'] =$data1['htel'];
                // $data['houses'] =$data1['houses'];
                // $data['area'] = $data1['area'];
                // $data['type'] = $data1['xiao'];
                $res = $this->charu_data($data);
                if($res=='ok'){//插入数据
                    
                    return "ok";
                }else if($res == 'yiyou'){
                    return "yiyou";
                }






              
            }else{
                return "非法注入";
            }
        }else{
             return "未传参数";
        }

        // echo $this->rsa->pubEncrypt('xiaohe asdas ');
        
        // privDecrypt
        // $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }


    public function charu_data($data){
        $pd =Db::name('ysjyy')->where(['tel'=>$data['tel'],'area'=>$data['area']])->find();
        if($pd){
            // dump($pd);
            return 'yiyou';
        }
        // dump($data);
        $data['createtime'] = time();//创建时间
        $data['status'] = 0;//未读
        
        $res =  Db::name('ysjyy')->insert($data);
        if($res){
            return 'ok';
        }
    }

}
